import { compose} from "rambda";
import { aperture, join, map, sum } from "ramda";
import { log, recursiveOf, take } from "./util";

const square = (x:number) => x * x
const sqrt = Math.sqrt

//过程式
const vlen_1 = (v:Array<number>) =>{ 
    let sum = 0;
    for(let i = 0; i<v.length; i++){
        sum += square(v[i])
    }
    return sqrt(sum)
 }
log(vlen_1([1,2,3]))

//--------------------------

//大众函数式
const vlen_2 = (v:Array<number>) =>
    sqrt(v.map(square).reduce((x,y)=>x+y))
console.log(vlen_2([1,2,3]))

//--------------------------

//经典函数式
const vlen_3 = compose(sqrt,sum,map(square))
console.log(vlen_3([1,2,3]))

//--------------------------

const nextLine = (prevLine: number[]) =>
    [1, ...aperture(2, prevLine).map(sum), 1]
const yanghui = recursiveOf(nextLine, [1])

console.log(
    take(5)(yanghui())
        .map(join(' '))
        .map((x, i) => ' '.repeat(5 - i) + x)
        .join('\n')
)

//aperture:用来计算数列的差
console.log(aperture(2, [1, 2, 3, 4, 5, 8]).map(([a, b]) => b - a))
console.log(aperture(2, [1, 2, 3, 4, 5, 8]).map(vlen_3))
console.log(take(5)(recursiveOf((x: number) => x * 2, 9)()))

